import pdb

import pyautogui as pyg
import pydirectinput as pyd
import pygetwindow as gw
import keyboard
import time
from Tools import Tools
# 打包方法 pyinstaller.exe --onefile 自动开盒.py
pause=False
shopping_pause=False
DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]
INIT_WIDTH=800
INIT_HEIGHT=600
def compute_position(x_offset,y_offset):
    y= DNF_WD.top+(INIT_HEIGHT/2+y_offset)*(DNF_WD.height/INIT_HEIGHT)
    x= DNF_WD.left+(INIT_WIDTH/2+x_offset)*(DNF_WD.width/INIT_WIDTH)
    return (x,y)

def click_dnf(x,y,duration=0.5):
    pyg.moveTo(x,y,duration=duration)
    pyg.mouseDown(button="left")
    pyg.mouseUp(button="left")

def pause():
    global pause
    pause =  True
    print("暂停开盒子")
def start():
    global pause
    pause =  False
    print("开始开盒子")
    openbox()
def openbox():
    pyd.press("4")
    #点击确认位置
    x,y = tool.compute_abs_position(373,350)
    #print(x,y)
    click_dnf(x,y,duration=0.01)
    time.sleep(0.02)
    x, y = tool.compute_abs_position(400, 400)
    click_dnf(x, y,duration=0.1)
    #print(x, y)
    time.sleep(0.02)

keyboard.add_hotkey("ctrl+num_8",start)
keyboard.add_hotkey("ctrl+num_9",pause)
print("1.按ctrl+num_8开始和ctrl+num_9结束")
print(DNF_WD, INIT_WIDTH, INIT_HEIGHT)
tool = Tools(DNF_WD, INIT_WIDTH, INIT_HEIGHT)
count = 0
while True:
    if pause:
        time.sleep(0.05)
    else:
        openbox()
        count =count +1
        print(f"开始开盒子：{count}")
